local love = require "love"
local enemy = require "Enemy"
local button = require "Button"

math.randomseed(os.time())

local player = {
    radius = 20,
    x = 30,
    y = 30
}

local game = {
    difficuty = 2,
    state = {
        menu = true,
        paused = false,
        running = false,
        ended = false,
    },
    points = 0,
    levels = {15, 30, 60, 120}
}

local enemies = {}

local buttons = {
    menu_state = {}
}

local function changeGameState(state)
    game.state.menu = (state == "menu")
    game.state.paused = (state == "paused")
    game.state.running = (state == "running")
    game.state.ended = (state == "ended")
end

local function startNewGame()
    changeGameState("running")
    game.points = 0
    enemies = {}
    for _ = 1, game.difficuty do table.insert(enemies, 1, enemy(game.difficuty)) end
end

function love.mousepressed(x, y, _button, _istouch, _presses)
    if game.state.menu then
        if _button == 1 then     -- left click
            for index in pairs(buttons.menu_state) do
                buttons.menu_state[index]:checkPressed(x, y)
            end
        end
    end
end

function love.load()
    love.window.setTitle("Save the ball")
    love.mouse.setVisible(false)

    buttons.menu_state.play_game = button("Play Game", startNewGame, nil, 80, 40)
    buttons.menu_state.settings = button("Settings", nil, nil, 80, 40)
    buttons.menu_state.exit_game = button("Exit Game", love.event.quit, nil, 80, 40)
end

function love.update(dt)
    player.x, player.y = love.mouse.getPosition()

    if game.state.running then
        for i = 1, #enemies do
            if not enemies[i]:checkTouched(player.x, player.y, player.radius) then
                enemies[i]:move(player.x, player.y)
                for j = 1, #game.levels do
                    if math.floor(game.points) == game.levels[j] then
                        for _ = 1, game.difficuty do table.insert(enemies, 1, enemy(game.difficuty)) end
                        game.points = game.points + 1
                    end
                end
            else
                changeGameState("menu")
            end
        end
        game.points = game.points + dt
    end
end

function love.draw()
    love.graphics.printf(
        "FPS: " .. love.timer.getFPS(),
        love.graphics.newFont(16), 10, love.graphics.getHeight() - 30,
        love.graphics.getWidth())

    if game.state.running then
        love.graphics.printf(math.floor(game.points), love.graphics.newFont(24), 0, 10, love.graphics.getWidth(), "center")
        for i = 1, #enemies do enemies[i]:draw() end
        love.graphics.setColor(1, 1, 1)
        love.graphics.circle("fill", player.x, player.y, player.radius)
    elseif game.state.menu then
        buttons.menu_state.play_game:draw(10, 20, 10, 10)
        buttons.menu_state.settings:draw(10, 70, 10, 10)
        buttons.menu_state.exit_game:draw(10, 120, 10, 10)

        love.graphics.setColor(1, 1, 1)
        love.graphics.circle("fill", player.x, player.y, player.radius / 2)
    end
end
